<?php

namespace App\Http\Requests\Api;

use Dingo\Api\Http\FormRequest;

class ProductsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        if ($this->isMethod('POST')) {
            return [
                'title' => [
                    'required',
                    'max:40',
                ],
                'subtitle' => [
                    'max:100',
                ],
                'contact_phone' => [
                    'required',
                    'regex:/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/',
                ],
                'image' => [
                    'required',
                ],
                'content' => [

                ],
                'schedules' => [
                    'array',
                    function ($attribute, $value, $fail) {
                        foreach ($value as $item) {
                            if (!array_key_exists('begin_time', $item)) {
                                $fail('begin_time is required');

                                return;
                            }
                            if (!array_key_exists('end_time', $item)) {
                                $fail('end_time is required');

                                return;
                            }
                            if ($item['begin_time'] <= date('Y-m-d H:i:s') || $item['end_time'] <= date('Y-m-d H:i:s')) {
                                $fail('begin_time or end_time could not earlier than now');

                                return;
                            }

                            if ($item['begin_time'] >= $item['end_time']) {
                                $fail('begin_time must be earlier than end_time');

                                return;
                            }

                            if (!array_key_exists('price', $item)) {
                                $fail('price is required');

                                return;
                            }

                            if (!is_numeric($item['price'])) {
                                $fail('price must be numeric');

                                return;
                            }
                        }
                    },
                ],
                'service_time' => [
                    'numeric',
                ],
                'price' => [
                    'numeric',
                ],
                'is_show' => [
                    'in:1,0',
                ],
                'province' => [
                    'required',
                    'exists:sys_province,name',
                ],
                'city' => [
                    'required',
                    'exists:sys_city,name',
                ],
                'district' => [
                    'required',
                    'exists:sys_district,name',
                ],
                'address' => [
                    'required',
                ],
                'longitude' => [
                    'required',
                    'numeric',
                ],
                'latitude' => [
                    'required',
                    'numeric',
                ],
            ];
        }

        if ($this->isMethod('PUT')) {
            return [
                'id' => [
                    'required',
                    'exists:products,id',
                ],
                'title' => [
                    'max:40',
                ],
                'subtitle' => [
                    'max:100',
                ],
                'contact_phone' => [
                    'regex:/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/',
                ],
                'image' => [
                ],
                'content' => [

                ],
                'service_time' => [
                    'numeric',
                ],
                'price' => [
                    'numeric',
                ],
                'is_show' => [
                    'in:1,0',
                ],
                'province' => [
                    'exists:sys_province,name',
                ],
                'city' => [
                    'exists:sys_city,name',
                ],
                'district' => [
                    'exists:sys_district,name',
                ],
                'address' => [
                ],
                'longitude' => [
                    'numeric',
                ],
                'latitude' => [
                    'numeric',
                ],
            ];
        }
    }

}
